<template>

  <div >
    <YBanner v-if="!isMobile" :data="bannerList" :classList="subjectList" :height="360"/>

    <div id="aCoursesList" :class="{ 'mobile-courses-list': isMobile }">
      <!-- 网校课程 开始 -->
      <div :class="{ 'mobile-course-container': isMobile }">
        <section class="container" :class="{ 'mobile-container': isMobile }">
          <header class="comm-title">
            <h2 class="tac">
              <span class="c-333">热门课程</span>
            </h2>
          </header>
          <div :class="{ 'mobile-course-content': isMobile }">
            <article class="comm-course-list" :class="{ 'mobile-course-list': isMobile }">
              <ul class="of" id="bna" :class="{ 'mobile-course-ul': isMobile }">
                <!--课程数据渲染-->
                <li v-for="(course, index) in courseList" v-bind:key="index" :class="{ 'mobile-course-li': isMobile }">
                  <div class="cc-l-wrap" :class="{ 'mobile-cc-l-wrap': isMobile }">
                    <section class="course-img" :class="{ 'mobile-course-img': isMobile }">
                      <img
                        :src="course.cover"
                        class="img-responsive"
                        :alt="course.title"
                        :class="{ 'mobile-course-cover': isMobile }"
                      >
                      <div class="cc-mask" :class="{ 'mobile-cc-mask': isMobile }">
                        <a :href="'/course/'+course.id" title="开始学习" class="comm-btn c-btn-1" :class="{ 'mobile-comm-btn': isMobile }">开始学习</a>
                      </div>
                    </section>
                    <h3 class="hLh30 txtOf mt10" :class="{ 'mobile-course-title-wrap': isMobile }">
                      <p class="course-title fsize15" :class="{ 'mobile-course-title': isMobile }">{{course.title}}</p>
                    </h3>
                    <section class="mt10 hLh20 of" :class="{ 'mobile-course-info': isMobile }">
                     <span class="fr jgTag bg-green" v-if="Number(course.price) === 0" :class="{ 'mobile-course-price-tag': isMobile }">
                    <i class="c-fff fsize12 f-fA">免费</i>
                    </span>
                      <span class="fr price" style="margin-right: 10px" v-else :class="{ 'mobile-course-price': isMobile }">
                        ￥{{course.price}}
<!--                    <i class="c-fff fsize12 f-fA" style="font-weight: 700!important;"> ￥{{course.price}}</i>-->
                    </span>
                      <span class="fl jgAttr c-ccc f-fA" style="padding: 0 10px" :class="{ 'mobile-course-stats': isMobile }">
                        <i class="c-999 f-fA">{{course.buyCount}}人学习</i>
                        |
                        <i class="c-999 f-fA">{{course.viewCount}}评论</i>
                      </span>
                    </section>
                  </div>
                </li>
              </ul>
              <div class="clear"></div>
            </article>
            <section class="tac pt20" :class="{ 'mobile-all-courses': isMobile }">
              <a href="/course" title="全部课程" class="comm-btn c-btn-2" :class="{ 'mobile-all-courses-btn': isMobile }">全部课程</a>
            </section>
          </div>
        </section>
      </div>
      <!-- /网校课程 结束 -->
      <!-- 网校名师 开始 -->
      <div :class="{ 'mobile-teacher-container': isMobile }">
        <section class="container" :class="{ 'mobile-container': isMobile }">
          <header class="comm-title">
            <h2 class="tac">
              <span class="c-333">名师大咖</span>
            </h2>
          </header>
          <div :class="{ 'mobile-teacher-content': isMobile }">
            <article class="i-teacher-list" :class="{ 'mobile-teacher-list': isMobile }">
              <ul class="of" :class="{ 'mobile-teacher-ul': isMobile }">
                <li v-for="(teacher,index) in teacherList" v-bind:key="index" :class="{ 'mobile-teacher-li': isMobile }">
                  <section class="i-teach-wrap" :class="{ 'mobile-i-teach-wrap': isMobile }">
                    <div class="i-teach-pic" :class="{ 'mobile-i-teach-pic': isMobile }">
                      <a :href='"/teacher/"+teacher.id' :title="teacher.name">
                        <img :alt="teacher.name" :src="teacher.avatar" :class="{ 'mobile-teacher-avatar': isMobile }">
                      </a>
                    </div>
                    <div class="mt10 hLh30 txtOf tac" :class="{ 'mobile-teacher-name-wrap': isMobile }">
                      <a :href='"/teacher/"+teacher.id' :title="teacher.name" class="fsize18 c-666" :class="{ 'mobile-teacher-name': isMobile }">{{teacher.name}}</a>
                    </div>
                    <div class="hLh30 txtOf tac" :class="{ 'mobile-teacher-career-wrap': isMobile }">
                      <span class="fsize14 c-999" :class="{ 'mobile-teacher-career': isMobile }">{{teacher.career}}</span>
                    </div>
                    <div class="mt15 i-q-txt" :class="{ 'mobile-teacher-intro-wrap': isMobile }">
                      <p class="c-999 f-fA" :class="{ 'mobile-teacher-intro': isMobile }">
                        {{teacher.intro}}
                      </p>
                    </div>
                  </section>
                </li>
              </ul>
              <div class="clear"></div>
            </article>
            <section class="tac pt20" :class="{ 'mobile-all-teachers': isMobile }">
              <a href="/teacher" title="全部讲师" class="comm-btn c-btn-2" :class="{ 'mobile-all-teachers-btn': isMobile }">全部讲师</a>
            </section>
          </div>
        </section>
      </div>
      <!-- /网校名师 结束 -->
    </div>
  </div>
</template>

<script>
  import banner from "@/api/banner"
  import index from '@/api/index'
  import YBanner from '~/components/Banner'
  export default {

    data() {
      return {
        //幻灯片配置
        swiperOption: {
          //配置分页
          autoHeight: true,
          pagination: {
            el: '.swiper-pagination'//分页的dom节点
          },
          //配置导航
          navigation: {
            nextEl: '.swiper-button-next',//下一页dom节点
            prevEl: '.swiper-button-prev'//前一页dom节点
          }
        },
        bannerList: [
          // {title:'test1',imageUrl:'https://szy-education.oss-cn-shenzhen.aliyuncs.com/banner/2021/06/18/1751315420f0491eb9f57bf22e937b0811.jpg',linkUrl:'/course'},
          // {title:'test2',imageUrl:'https://szy-education.oss-cn-shenzhen.aliyuncs.com/banner/2021/06/18/45966b9aef2b4cf59cc7275699c8e70812.jpg',linkUrl:'/course'},
          // {title:'test3',imageUrl:'https://szy-education.oss-cn-shenzhen.aliyuncs.com/banner/2021/06/18/2a8ea7c2e96a4c47855c715c2643e92713.jpg',linkUrl:'/course'},
        ],
        teacherList: {},
        courseList: {},
        //banner上的分类

        subjectList:{},
        isMobile: false
      }
    },
    components: {
      YBanner
    }, 
    created() {
      console.log("classList",this.classList)
      this.initDataBanner()
      this.initDataObj()
      // 在created中不直接调用checkIsMobile，因为可能在服务端执行
    },
    mounted() {
      // 添加窗口大小改变事件监听器
      this.checkIsMobile()
      window.addEventListener('resize', this.checkIsMobile)
    },
    beforeDestroy() {
      // 移除事件监听器
      if (process.client) {
        window.removeEventListener('resize', this.checkIsMobile)
      }
    },
    methods: {
      initDataBanner() {
        banner.getList().then(response => {
          this.bannerList = response.data.list
          console.log(this.bannerList)
        })
      },
      initDataObj() {
        index.getList().then(response => {
          this.teacherList = response.data.teacherList
          this.courseList = response.data.courseList
          this.subjectList= response.data.subjectList
          console.log( this.subjectList)
        })
      },
      checkIsMobile() {
        // 确保只在客户端执行
        if (process.client) {
          // 检查屏幕宽度是否小于768px
          this.isMobile = window.innerWidth < 768
        }
      }
    }
  }
</script>
<style scoped>
/* 热门课程部分响应式优化 */
@media (max-width: 1200px) {
  .comm-course-list li {
    width: 33.33%;
  }
}

@media (max-width: 992px) {
  .comm-course-list li {
    width: 50%;
  }
  
  .cc-l-wrap {
    margin: 0 12px 24px 0;
  }
}

@media (max-width: 768px) {
  .comm-course-list li {
    width: 50%;
  }
  
  .cc-l-wrap {
    width: 100%;
    margin: 0 10px 20px 0;
  }
  
  .course-img {
    height: 120px;
  }
  
  .img-responsive {
    height: 120px;
    margin-bottom: 5px;
    width: 100%;
    object-fit: cover; /* 确保图片自适应 */
  }
  
  .course-title {
    font-size: 14px !important;
    height: 40px;
    line-height: 20px;
    word-break: break-all; /* 防止标题过长 */
  }
  
  .mt10 {
    margin-top: 5px !important;
  }
  
  .mt15 {
    margin-top: 10px !important;
  }
  
  .hLh20 {
    height: auto;
    line-height: 1.4;
  }
  
  .fsize18 {
    font-size: 16px !important;
  }
  
  .fsize14 {
    font-size: 12px !important;
  }

  .container, .mobile-container {
    width: 100%; /* 确保容器宽度自适应 */
    padding: 0 10px; /* 增加内边距以防止内容贴边 */
    box-sizing: border-box; /* 确保内边距和边框包含在总宽度内 */
  }

  .comm-course-list, .i-teacher-list {
    overflow-x: hidden; /* 避免水平滚动 */
  }
}

@media (max-width: 576px) {
  .comm-course-list li {
    width: 100%;
  }
  
  .comm-course-list li .cc-l-wrap {
    margin: 0 0 20px 0;
  }
  
  #bna {
    margin-left: 0;
  }
  
  .container, .mobile-container {
    width: 100%; /* 确保容器宽度自适应 */
    padding: 0 10px; /* 增加内边距以防止内容贴边 */
    box-sizing: border-box; /* 确保内边距和边框包含在总宽度内 */
  }
  
  .comm-title h2 span {
    font-size: 20px;
  }
  
  .pt20 {
    padding-top: 15px !important;
  }
  
  .c-btn-2 {
    width: 100%;
    height: 32px;
    line-height: 32px;
    font-size: 16px;
    display: block; /* 确保按钮在小屏幕上全宽显示 */
  }

  .comm-course-list, .i-teacher-list {
    overflow-x: hidden; /* 避免水平滚动 */
  }
}

/* 名师大咖部分响应式优化 */
@media (max-width: 1200px) {
  .i-teacher-list ul li {
    width: 33.33%;
  }
}

@media (max-width: 992px) {
  .i-teacher-list ul li {
    width: 50%;
  }
}

@media (max-width: 768px) {
  .i-teacher-list ul li {
    width: 50%;
  }
  
  .i-teach-wrap {
    margin: 0 0 30px 0;
    padding: 10px;
  }
  
  .i-teach-pic {
    width: 70%;
  }
  
  .i-teach-pic img {
    border-radius: 50%;
    width: 100%;
    height: auto;
    object-fit: cover; /* 确保图片自适应 */
  }
  
  .mt10 {
    margin-top: 5px !important;
  }
  
  .hLh30 {
    height: auto;
    line-height: 1.4;
  }
  
  .txtOf {
    height: auto;
  }
}

@media (max-width: 576px) {
  .i-teacher-list ul li {
    width: 100%;
    float: none;
  }
  
  .i-teach-wrap {
    margin: 0 0 20px 0;
  }
  
  .i-teach-pic {
    width: 100%; /* 使图片全宽显示 */
    margin: 10px auto 0;
  }
  
  .i-teach-pic img {
    border-radius: 50%;
    width: 100%;
    height: auto;
    object-fit: cover; /* 确保图片自适应 */
  }
  
  .mt15 {
    margin-top: 10px !important;
  }
  
  .i-q-txt {
    font-size: 12px;
    padding: 0 5px;
  }
}

/* 通用响应式优化 */
.tac {
  text-align: center;
}

.clearfix::after {
  content: "";
  display: table;
  clear: both;
}

.fl {
  float: left;
}

.fr {
  float: right;
}

.clear {
  clear: both;
}

@media (max-width: 768px) {
  .comm-title {
    margin: 20px 0 15px;
  }
  
  .comm-title h2 {
    font-size: 22px;
  }
  
  #aCoursesList {
    padding-bottom: 40px;
  }
}

@media (max-width: 480px) {
  .comm-title {
    margin: 15px 0 10px;
  }
  
  .comm-title h2 {
    font-size: 20px;
  }
  
  .c-333 {
    font-size: 18px;
  }
}
</style>
